﻿namespace Yom.WebService.Service
{
    using System;
    using System.IO;
    using System.Web;
    using Yom.Common;

    public class FileUpload
    {
        public void OutWrite()
        {
            string Mod = Factory.Request("Mod");
            string CID = Factory.Request("CID");
            string VID = Factory.Request("VID");
            HttpFileCollection files = HttpContext.Current.Request.Files;
            HttpResponse Response = HttpContext.Current.Response;
            HttpPostedFile oFile = null;
            try
            {
                oFile = files[0];
            }
            catch
            {
                Response.Write("102");
                Response.End();
            }
            Response.Cache.SetNoStore();
            Response.ContentType = "text/html";
            string str;
            string str2;
            switch (Mod)
            {
                case "Editor":
                    str = "0";
                    str2 = string.Empty;
                    if (SiteLoad.Admin.AdminID <= 0)
                        str = "101";
                    else if (oFile == null)
                        str = "102";
                    else
                    {
                        if (oFile.ContentLength == 0)
                            str = "102";
                        else if (!Factory.CheckFile(Factory.FilterFile(Path.GetExtension(oFile.FileName).ToLower()), "jpg,gif,jpeg,png,bmp,rar,zip,tar,gz,txt"))
                            str = "202";
                        else
                        {
                            string str3 = "Files/Attach/Month_" + DateTime.Now.ToString("yyMM");
                            string path = Path.Combine(Factory.RootPath, str3);
                            if (!Directory.Exists(path))
                                Directory.CreateDirectory(path);
                            string str5 = DateTime.Now.ToString("yyMM") + "_" + (DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString()).ToMd5() + Path.GetExtension(oFile.FileName);
                            oFile.SaveAs(Path.Combine(path, str5));
                            str2 = SiteLoad.Scheme.SiteUrl + "/" + str3 + "/" + str5;
                        }
                    }
                    Response.Write("<script type=\"text/javascript\">window.parent.OnUploadCompleted(" + str + ",'" + str2 + "') ;</script>");
                    break;
                case "Face":
                    str = "#JN&UF*#$Y@&*#";
                    str2 = string.Empty;

                    if (oFile == null)
                        str = "102";
                    else
                    {
                        if (oFile.ContentLength == 0)
                            str = "102";
                        else if (!Factory.CheckFile(Factory.FilterFile(Path.GetExtension(oFile.FileName).ToLower()), "jpg,gif,jpeg,png,bmp"))
                            str = "202";
                        else
                        {
                            string str3 = SiteLoad.Scheme.SaveFace;
                            string path = Path.Combine(Factory.RootPath, str3);
                            if (!Directory.Exists(path))
                                Directory.CreateDirectory(path);
                            string str5 = DateTime.Now.ToString("yyMM") + "_" + (DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString()).ToMd5() + Path.GetExtension(oFile.FileName);
                            oFile.SaveAs(Path.Combine(path, str5));
                            str2 = str3 + "/" + str5;
                        }
                    }
                    if (str != "#JN&UF*#$Y@&*#")
                        Response.Write(str);
                    else
                        Response.Write(str2);
                    break;
                case "Chapter":
                    str = "#JN&UF*#$Y@&*#";
                    str2 = string.Empty;
                    if (!string.IsNullOrEmpty(CID) && !string.IsNullOrEmpty(VID))
                    {
                        try
                        {
                            for (int iFile = 0; iFile < files.Count; iFile++)
                            {
                                HttpPostedFile PostedFile = files[iFile];

                                if (PostedFile == null)
                                    str += PostedFile.FileName + "文件为空";
                                else
                                {
                                    if (PostedFile.ContentLength == 0)
                                        str += PostedFile.FileName + "文件为空";
                                    else if (!Factory.CheckFile(Factory.FilterFile(Path.GetExtension(PostedFile.FileName).ToLower()), "jpg,gif,jpeg,png,bmp"))
                                        str += PostedFile.FileName + "为不允许文件类型";
                                    else
                                    {
                                        string str3 = "Files/Cartoon/" + CID + "/" + VID;
                                        string path = Path.Combine(Factory.RootPath, str3);
                                        if (!Directory.Exists(path))
                                            Directory.CreateDirectory(path);
                                        string str5 = DateTime.Now.ToString("yyMM") + "_" + (DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString()).ToMd5() + Path.GetExtension(PostedFile.FileName);
                                        PostedFile.SaveAs(Path.Combine(path, str5));
                                        Yom.Model.Chapter model = new Yom.Model.Chapter();
                                        model.ImgPath = str3 + "/" + str5;
                                        model.VolumeID = VID.ToLong();
                                        if (new Yom.Data.Chapter().Add(model))
                                            str2 = PostedFile.FileName + "上传成功<br />";
                                    }
                                }
                            }
                        } catch { }
                        if (str != "#JN&UF*#$Y@&*#")
                            Response.Write(str);
                        else
                            Response.Write(str2);
                    }
                    break;
                case "ChapterEdit":
                    str = "#JN&UF*#$Y@&*#";
                    str2 = string.Empty;
                    if (!string.IsNullOrEmpty(VID))
                    {
                        string cid = new Yom.Data.Volume().GetModel(VID.ToLong()).ComicID.ToString();
                        if (oFile == null)
                            str += oFile.FileName + "文件为空";
                        else
                        {
                            if (oFile.ContentLength == 0)
                                str += oFile.FileName + "文件为空";
                            else if (!Factory.CheckFile(Factory.FilterFile(Path.GetExtension(oFile.FileName).ToLower()), "jpg,gif,jpeg,png,bmp"))
                                str += oFile.FileName + "为不允许文件类型";
                            else
                            {
                                string str3 = "Files/Cartoon/" + cid + "/" + VID;
                                string path = Path.Combine(Factory.RootPath, str3);
                                if (!Directory.Exists(path))
                                    Directory.CreateDirectory(path);
                                string str5 = DateTime.Now.ToString("yyMM") + "_" + (DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString()).ToMd5() + Path.GetExtension(oFile.FileName);
                                oFile.SaveAs(Path.Combine(path, str5));
                                str2 = str3 + "/" + str5;
                            }
                        }
                        if (str != "#JN&UF*#$Y@&*#")
                            Response.Write(str);
                        else
                            Response.Write(str2);
                    }
                    break;
                default: break;
            }
        }
    }
}